CHAPTER 4 : The Class Editor |
A Java application consists of one or more class files. Classes contain variables and methods. It is the methods in the classes which perform the functions of the application. With Visaj you can construct classes containing methods which create the user interface of your application. Classes are defined using the Class Editor. The Class Editor is the first window displayed when you run Visaj. See the Integration with an IDE section on page 161 for details on how you can start Visaj from your IDE (Integrated Development Environment).
The Class Editor, shown in Figure 4-1, consists of a menubar, a toolbar, a tabbed panel on the left of the window and a tree view of the class structure on the right.
A tree structure of the class is shown on the right side of the Class Editor window. Each method in the tree can be selected by clicking over it. The area on the left of the Class Editor displays three sets of editable information for the currently selected method:
1. | Bean hierarchy |
2. | Event bindings |
3. | Method signature |
These are described in the Method Editing section on page 47.
Add and delete methods from the list of class methods, shown in Figure 4-2, by selecting the relevant item from the "Method" menu.
Methods shown with a bean icon are methods which are capable of creating or editing a bean - that is, they have a containment hierarchy associated with them. Methods without the bean icon would, typically, perform a non-interface operation such as the "myExit" method in the tutorial in Chapter 3 which simply exits the application.
Visaj automatically gives you one method when you start a new class. This method is the default constructor. Change this to a simple class method in the Method Signature page of the Class Editor, described in the Editing the Method Signature section on page 48. Any method can be made the constructor by setting the appropriate toggle on the Method Signature page.
The "Properties..." item in the Class menu displays the dialog shown in Figure 4-3.
This dialog allows you to edit the properties of the class. You can change its name, access and superclass. By default, the access type of the class is public, its name is "MyClass" and its superclass is java.lang.Object. You may also provide a list of interfaces which this class implements.
The class name is used as the filename of the Java source file when you generate Java. This is a requirement of the Java language. If you generate Java, change the class name and then regenerate code, the new class name is used as the filename of the new Java source file. In addition, if you have "Update existing files" selected in the Generate dialog, the previous Java source file is removed. This is to ensure that the generated source is always in step with the design since you have requested an "Update". If you wish to retain your previous file, make sure that the "Update existing files" toggle is not selected. In this case, however, any changes you may have made to the generated source file are not retained.
Selecting the "Package" tab in the Class Properties dialog allows you to specify the package in which your class is found and any packages it imports.
Populate your class with new methods by selecting the "Add method" item from the Class menu of the Class Editor.
There are two types of method which may be added to your class automatically by Visaj:
1. | Exception methods |
2. | Interface methods |
Exception methods are added to your class when you create an event binding using a method which can throw an exception. The exception handler matches the type of exception which can be thrown, as shown in Figure 4-4. In this case, the event binding method handler was the "setPage" method of the Swing component, JEditorPane. This method can throw an IOException.
These are convenience methods added by Visaj. If an exception is thrown, this method is called. You can safely add code for handling the exception - the code is retained if you choose "Update existing files" in the Generate dialog. You cannot edit these methods (except to make them abstract) because they are expected to have a particular signature. Although you can remove these methods (by selecting "Delete" from the Method menu), they reappear in your class structure editor when code is generated if they are still referenced anywhere in the generated code.
If you change the properties of your class (by choosing "Properties" from the Class menu) so that it implements an interface, the methods of the interface appear in the class structure editor. These methods must be generated otherwise your application will not compile. You cannot edit these methods except to make them abstract.
this
is a keyword in the Java language and refers to "this object" - that is, an instance of the current class. For example, in the following piece of code the keyword this
disambiguates between the class variable width
and the parameter width
which is passed into the constructor:
class Example {
int width;
public Example(int width) {
this.width = width;
}
}
this
is selectable from the palette. The type of this
is whatever has been defined as the superclass of the current class. By default, the superclass is Object
. The palette icon, Property Sheet and behavior all reflect the superclass. When selected from the palette, this
is added to the design as you would expect from its type. So, if the superclass is derived from java.awt.Component
or MenuComponent
, this
is added to the hierarchy. If it is not (an instance of java.lang.Object
or java.awt.Color
, for example), then this
appears in the invisible bean area.
The use of this
can be explained more fully with a simple example. Try the following, assuming you have a fresh Class Editor window in front of you:
1. | Specify Frame as the superclass by selecting "Properties..." from the Class menu and filling in the dialog as shown in Figure 4-5. |
The Class Structure now tells you that the superclass of your current class is Frame
and the palette icon has changed appropriately, as shown in Figure 4-6.
this
as a Frame
2. | Select this from the palette and add a Label and a Button to it, as shown in Figure 4-7. |
this is a Frame, so you can add children to it. |
this
in a Hierarchy
3. | Generate code for your small design. |
See the How to Generate Code section on page 151 if you are not sure how to do this. |
4. | Look at the code which has been generated. |
The relevant part of that code is shown below. |
The code fragment listed below shows that the Label and Button components have been added to the class itself and initialized in the constructor, which is the method in which they were defined in Visaj.
public class MyClass extends java.awt.Frame {
//vj- <VJ-BeginClassDef>
//vj+ <VJ-DataMembers>
protected Label label1 ;
protected Button button1 ;
//vj- <VJ-DataMembers>
//vj= <VJ-Methods>
//vj+ <VJ-BeginMethodDef>
// Method# 1
public MyClass() {
//vj- <VJ-BeginMethodDef>
//vj= <VJ-MethodCode>
//vj+ <VJ-DefineAWTMembers>
this.setTitle( "null" );
label1 = new Label();
label1.setText( "label1" );
button1 = new Button();
button1.setLabel( "button1" );
{
String strConstraint;
strConstraint = "Center";
this.add(label1, strConstraint, -1);
strConstraint = "North";
this.add(button1, strConstraint, -1);
}
this.pack();
this.show();
...
For comparison, the following code fragment shows what is generated when a Frame is selected from the palette, instead of this
:
public class MyClass extends java.awt.Frame {
//vj- <VJ-BeginClassDef>
//vj+ <VJ-DataMembers>
protected Frame frame1 ;
protected Label label1 ;
protected Button button1 ;
//vj- <VJ-DataMembers>
//vj= <VJ-Methods>
//vj+ <VJ-BeginMethodDef>
// Method# 1
public MyClass() {
//vj- <VJ-BeginMethodDef>
//vj= <VJ-MethodCode>
//vj+ <VJ-DefineAWTMembers>
frame1 = new Frame();
frame1.setTitle( "frame1" );
label1 = new Label();
label1.setText( "label1" );
button1 = new Button();
button1.setLabel( "button1" );
{
String strConstraint;
strConstraint = "Center";
frame1.add(label1, strConstraint, -1);
strConstraint = "North";
frame1.add(button1, strConstraint, -1);
}
frame1.pack();
frame1.show();
...
Using this
in a method design is of most use for classes that are derived from AWT components, as in our example above. This allows methods to add components to the base component, which is the class itself. In such a case, you would normally use the constructor, as we have done in this example. Then, when an instance of the class is created, the components within it are created too.
You may have only one instance of "this" per method. If you change the superclass of the current class after having added "this" to any methods, they become simple variables of the type they were when added to the method. That is, if "this" is added to the containment hierarchy when the superclass was a Panel and the superclass is now a Dialog, "this" becomes a simple Panel and retains its position in the hierarchy and any properties which have been set on it.
The left area of the Class Editor contains a tabbed panel which provides three editors for a method. Select a method from the class structure on the right to view and edit information on that method. The three editors are:
1. | Beans (the user interface builder) |
2. | Event bindings |
3. | Method signature |
The user interface builder, described in Chapter 5, "Beans View", starting on page 55, allows you to build the user interface Beans and invisible Beans of your application. This is a powerful graphical building tool complete with properties, layouts and a true representation of the user interface.
Event bindings are connections between Java beans. They provide a quick way of adding functionality to your user interface. They take effect immediately in the dynamic display and are generated into the code. Use the Event Binding Editor to link the action of one bean to a method in another. This is fully described in Chapter 6, "Event Bindings", starting on page 79.
Editing a method signature is explained in the following section.
Pressing the "Method Editors only" button on the toolbar hides the Class Structure View, thereby allowing more space for the design of your methods. Pressing the "Show both" button on the toolbar makes the Class Structure reappear.
Selecting the "Signature" tab on the left of the Class Editor displays the panel shown in Figure 4-8.
Use this panel to modify the method declaration for the method that has been selected, and is shown highlighted in the list. You can change a method's name, its return type, select one or more of the modifiers static, final, synchronized, native or abstract, change the method' s access type from "public" to protected, private or "default" no specified type.
If you select the modifier "native" or "abstract", then the method cannot contain code, and the Beans and Event pages are disabled. If you already have beans in this method, you cannot set either of these toggles.
If you make any method in the class "abstract", then the class itself is also abstract.
The two lists in the lower area of the panel allow you to give the method parameters, or arguments, and to state which exceptions are to be thrown by the method.
Note that all three of the type fields here (and the superclass field in the class properties dialog) do not require a full class name, if the class is in one of the base java packages, or in Swing. You need only type "IOException" and it will resolve this as "java.io.IOException".
In this panel, and in the method list, blue indicates a Java keyword and red either a built-in Java type, or a class.
You can tell Visaj whether or not to generate a main method using the "Main method" item in the Method menu. The possible options are:
1. | You have not set the "Main method" checkbox for any of the methods in your class. No main method is generated. |
2. | The constructor has the "Main method" item set for it. This results in just the constructor being called from the generated main method. |
3. | A method other than the constructor has the "Main method" checkbox set. The generated main method calls this method after calling the constructor. |
A method selected in this way is indicated by a star in the Class Structure View.
Visaj can import designs created in X-Designer, the graphical user interface builder for Motif. This enables you to move legacy Motif C/C++ designs quickly to Java. Visaj also imports designs created in Sun Microsystems' Workshop Visual in exactly the same way.
The File menu contains an Import pullright menu. This contains one item: "X-Designer bridge file". Selecting this displays a file dialog allowing you to specify the design to import.
There are some features in X-Designer which have no equivalent in Visaj. If your import file contains any of these, a dialog is displayed describing the situation. The possible messages are:
1. | One or more classes were expanded into simple component hierarchies. |
2. | String/font/color/pixmap objects in your design were expanded into simple property settings. |
3. | Forms were converted to null layouts. |
Each of these is described in the following sub-sections.
Apart from the discrepancies described above, the imported design should reflect the Motif application exactly.
In X-Designer, you can make any component a class. In Visaj, to break your design into classes you build separate hierarchies in different designs, generate code, compile it and then add the generated hierarchies to the palette as beans for use in larger designs.
Converting from the X-Designer model to the Visaj model is better done under your control than in an automated import function, as the compiler/debugger/etc. environment can vary greatly between users.
So, while importing, Visaj `expands' classes, basically ignoring whether or not any given component is a class. You can cut sub-hierarchies and paste them into new Visaj designs as you see fit after importing the design.
Visaj has no equivalent to these (or for that matter, no equivalent to pixmap objects - but see above). So, all references to objects are expanded into simple resource values. For example, if you have a label l whose text is <fred>, <fred> being a string object with value "Hello!", then after import, l will have the text "Hello!", and <fred> will have been forgotten.
The layout manager com.pacist.mwt.FormLayoutManager
is not supported. Components whose layout is controlled by this manager are positioned absolutely as a result. You are recommended to change your design to use one of the Java layout managers.
Make your class into an applet by specifying java.applet.Applet
as the superclass, as described in the Editing Properties of the Class section on page 40. Having done this, the "this" icon on the object palette changes to the applet icon. Select "this" from the palette to add objects to your applet.
If you are using Swing, you can make your class into a JApplet from the Swing component set by making javax.swing.JApplet
the superclass.
To generate source code for your class, select "Generate Java..." from the Generate menu or press the Generate button on the toolbar. Code generation is described in Chapter 11, "Generated Code", starting on page 151.
As well as providing a way of displaying a new window onto your Visaj save file and of moving between all open Visaj windows, the Windows menu allows you to display the Font and Color Selectors.
The Font Selector, shown in Figure 4-9, provides a means of selecting a font.
To set the font of a component, first display the component's property sheet. You can then use drag and drop: either drag the selected Font from the Selector (using the mouse button) and drop it directly over a property of type "Font" or select the property first and then drop the font into the area at the bottom of the property sheet where the current value is being displayed. Alternatively you may copy (Ctrl+C) from the sample area and paste (Ctrl+V) into the property sheet editing area.
The Color Selector, shown in Figure 4-10, is a dialog with tabbed panels providing different color viewing models. Some viewing models require you to click over a color to select it, some provide sliders. The color you have most recently selected appears on the right of the sample area at the top of the Selector window. The left of this area shows the original color.
To set a component's color, display its property sheet, select the property which is expecting the color, drag your chosen color from the sample area of the Color Selector (with the mouse button) and drop it either directly into the property, as displayed in the sheet, or into the editing area at the bottom of the property sheet. Alternatively you may copy (Ctrl+C) from the sample area and paste (Ctrl+V) into the property sheet editing area.
To display Visaj's other tools, either select one of the "New" options from the File menu or the toolbar, or open a saved file of the appropriate editor.
The Option menu contains items for your general use of Visaj. There are two items: "Authentication..." and "Java Console...". Selecting "Authentication..." displays the Licensing dialog which is described in the accompanying document on licensing. If you are using Visaj from an IDE (Integrated Development Environment), this option is disabled as licensing is controlled by the IDE and not by Visaj.
Selecting "Java Console..." displays a simple window containing a read-only text area. This window displays any exceptions which have occurred while you are using Visaj. You only need to check here if you feel that Visaj is not responding correctly. This window is for the display of information only.